memorycard.js ➔ ???   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 41

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 7
Bugs 0 Features 0
Metric Value
c 7
b 0
f 0
nc 1
dl 0
loc 41
ccs 4
cts 4
cp 1
rs 8.8571
nop 0
cc 1
crap 1
1
/**
2
 * A module for a standard playing card.
3
 *
4
 * @module
5
 */
6
"use strict";
7
8
class Memorycard {
9
    /**
10
     * @constructor
11
     */
12
    constructor() {
13 24
        this.cards = new Map([
14
            ["00", "alpaca.png"], ["10", "alpaca.png"],
15
            ["20", "giraff.png"], ["30", "giraff.png"],
16
            ["01", "monkeys.png"], ["11", "monkeys.png"],
17
            ["21", "panda.png"], ["31", "panda.png"],
18
            ["02", "puppy.png"], ["12", "puppy.png"],
19
            ["22", "ram.png"], ["32", "ram.png"],
20
            ["03", "wolf.png"], ["13", "wolf.png"],
21
            ["23", "squirrel.png"], ["33", "squirrel.png"],
22
            ["04", "fox.png"], ["14", "fox.png"],
23
            ["24", "bear.png"], ["34", "bear.png"],
24
        ]);
25 24
        this.cardvalues = [
26
            "alpaca.png", "alpaca.png",
27
            "giraff.png", "giraff.png",
28
            "monkeys.png", "monkeys.png",
29
            "panda.png", "panda.png",
30
            "puppy.png", "puppy.png",
31
            "ram.png", "ram.png",
32
            "wolf.png", "wolf.png",
33
            "squirrel.png", "squirrel.png",
34
            "fox.png", "fox.png",
35
            "bear.png", "bear.png"
36
        ];
37 24
        this.positions = [
38
            "00", "10", "20", "30",
39
            "01", "11", "21", "31",
40
            "02", "12", "22", "32",
41
            "03", "13", "23", "33",
42
            "04", "14", "24", "34"
43
        ];
44
        // this.cardvalue              = [1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6];
45
        // this.defaultcardpositions   = [
46
        //     "00", "10", "20", "30",
47
        //     "01", "11", "21", "31",
48
        //     "02", "12", "22", "32"
49
        // ];
50
        // this.cardpositions  = this.defaultcardpositions;
51 24
        this.numOfCards     = 20;
52
    }
53
54
    /**
55
     * Get value of memorycard based on its position.
56
     *
57
     * @param {string} position - The positin of the memorycard.
58
     *
59
     * @returns {string} value of memorycard.
60
     */
61
    getCardValue(position) {
62 12
        var value = this.cards.get(position);
63
64
        // console.log("Inne i getCardValue");
65
        // console.log(position);
66 12
        return value;
67
    }
68
69
    /**
70
    * Get positions of cards with certain value
71
    *
72
    * @param {string} cardvalue
0 ignored issues
show
Documentation introduced by
The parameter cardvalue does not exist. Did you maybe forget to remove this comment?
Loading history...
73
    *
74
    */
75
    getPairPositions(pairvalue) {
76
        var pairpositions = [];
77
78
        this.cards.forEach(function(cardvalue, position, cards) {
79
            console.log(cards);
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
80 4
            if (cardvalue == pairvalue) {
81
                pairpositions.push(position);
82
            }
83
        });
84
        return pairpositions;
85
    }
86
87
    /**
88
     * Get id of memorycard based on value.
89
     *
90
     * @param {integer} id - The id of the card.
91
     *
92
     * @returns {array} possible id:s of card with value in format [id, id]
93
     */
94
    // getCardId(value) {
95
    //     if (value < 1 || value >= this.cardvalue[this.numOfCards - 1]) {
96
    //         return [-1, -1];
97
    //     }
98
    //
99
    //     var result = [];
100
    //     var pos = this.cardvalue.indexOf(value);
101
    //
102
    //     // walking cardvalue and adding posiiton to result[] where correct value
103
    //     while (result.indexOf(pos) === -1) {
104
    //         result.push(pos);
105
    //         pos = this.cardvalue.indexOf(value, pos + 1);
106
    //     }
107
    //
108
    //     // result contains positions and -1. Splice to take away -1.
109
    //     result.splice(2, 1);
110
    //
111
    //     return result;
112
    // }
113
114
    /**
115
     * Is two memorycards a pair.
116
     *
117
     * @param {integer} id - The id of the card.
0 ignored issues
show
Documentation introduced by
The parameter id does not exist. Did you maybe forget to remove this comment?
Loading history...
118
     *
119
     * @returns {booelan} true if pair, false if not.
120
     */
121
    isPair(posone, postwo) {
122
        // if (cardone < 0 || cardone >= this.numOfCards) {
123
        //     return undefined;
124
        // }
125
        // if (cardtwo < 0 || cardtwo >= this.numOfCards) {
126
        //     return undefined;
127
        // }
128
129 8
        return this.cards.get(posone) == this.cards.get(postwo);
130
    }
131
132
    /**
133
    * Place memorycards = set this.cardpositions.
134
    */
135
    placeCards(random = true) {
136 4
        if (random) {
137 2
            this.shuffle();
138
        }
139
140 4
        for (let i = 0; i < this.positions.length; i++) {
141 80
            this.cards.set(this.positions[i], this.cardvalues[i]);
142
        }
143
    }
144
145
    shuffle() {
146 2
        var n = this.numOfCards,
147
            temp,
148
            i;
149
150
        // While there remain elements to shuffle…
151 2
        while (n) {
152
            // Pick a remaining element…
153 40
            i = Math.floor(Math.random() * n--);
154
155
            // And swap it with the current element.
156 40
            temp = this.cardvalues[n];
157 40
            this.cardvalues[n] = this.cardvalues[i];
158 40
            this.cardvalues[i] = temp;
159
        }
160
    }
161
162
    getCardValues() {
163
        return [...this.cards.values()];
164
    }
165
}
166
167
// Export module
168
module.exports = Memorycard;
169